package org.jivesoftware.smackx.provider;

import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.FormField.Option;
import org.jivesoftware.smackx.packet.DataForm;
import org.jivesoftware.smackx.packet.DataForm.Item;
import org.jivesoftware.smackx.packet.DataForm.ReportedData;
import org.xmlpull.v1.XmlPullParser;

public class DataFormProvider
  implements PacketExtensionProvider
{
  private FormField parseField(XmlPullParser paramXmlPullParser)
    throws Exception
  {
    int i = 0;
    FormField localFormField = new FormField(paramXmlPullParser.getAttributeValue("", "var"));
    localFormField.setLabel(paramXmlPullParser.getAttributeValue("", "label"));
    localFormField.setType(paramXmlPullParser.getAttributeValue("", "type"));
    while (true)
    {
      int j;
      label170: 
      do
        while (true)
        {
          do
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  if (i != 0)
                    return localFormField;
                  j = paramXmlPullParser.next();
                  if (j != 2)
                    break label170;
                  if (!(paramXmlPullParser.getName().equals("desc")))
                    break;
                  localFormField.setDescription(paramXmlPullParser.nextText());
                }
                if (!(paramXmlPullParser.getName().equals("value")))
                  break;
                localFormField.addValue(paramXmlPullParser.nextText());
              }
              if (!(paramXmlPullParser.getName().equals("required")))
                break;
              localFormField.setRequired(true);
            }
          while (!(paramXmlPullParser.getName().equals("option")));
          localFormField.addOption(parseOption(paramXmlPullParser));
        }
      while ((j != 3) || (!(paramXmlPullParser.getName().equals("field"))));
      i = 1;
    }
  }

  private DataForm.Item parseItem(XmlPullParser paramXmlPullParser)
    throws Exception
  {
    int i = 0;
    ArrayList localArrayList = new ArrayList();
    while (true)
    {
      int j;
      label66: 
      do
        while (true)
        {
          do
          {
            if (i != 0)
              return new DataForm.Item(localArrayList);
            j = paramXmlPullParser.next();
            if (j != 2)
              break label66;
          }
          while (!(paramXmlPullParser.getName().equals("field")));
          localArrayList.add(parseField(paramXmlPullParser));
        }
      while ((j != 3) || (!(paramXmlPullParser.getName().equals("item"))));
      i = 1;
    }
  }

  private FormField.Option parseOption(XmlPullParser paramXmlPullParser)
    throws Exception
  {
    int i = 0;
    FormField.Option localOption = null;
    String str = paramXmlPullParser.getAttributeValue("", "label");
    while (true)
    {
      int j;
      label69: 
      do
        while (true)
        {
          do
          {
            if (i != 0)
              return localOption;
            j = paramXmlPullParser.next();
            if (j != 2)
              break label69;
          }
          while (!(paramXmlPullParser.getName().equals("value")));
          localOption = new FormField.Option(str, paramXmlPullParser.nextText());
        }
      while ((j != 3) || (!(paramXmlPullParser.getName().equals("option"))));
      i = 1;
    }
  }

  private DataForm.ReportedData parseReported(XmlPullParser paramXmlPullParser)
    throws Exception
  {
    int i = 0;
    ArrayList localArrayList = new ArrayList();
    while (true)
    {
      int j;
      label66: 
      do
        while (true)
        {
          do
          {
            if (i != 0)
              return new DataForm.ReportedData(localArrayList);
            j = paramXmlPullParser.next();
            if (j != 2)
              break label66;
          }
          while (!(paramXmlPullParser.getName().equals("field")));
          localArrayList.add(parseField(paramXmlPullParser));
        }
      while ((j != 3) || (!(paramXmlPullParser.getName().equals("reported"))));
      i = 1;
    }
  }

  public PacketExtension parseExtension(XmlPullParser paramXmlPullParser)
    throws Exception
  {
    int i = 0;
    DataForm localDataForm = new DataForm(paramXmlPullParser.getAttributeValue("", "type"));
    while (true)
    {
      int j;
      label172: 
      do
        while (true)
        {
          do
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    if (i != 0)
                      return localDataForm;
                    j = paramXmlPullParser.next();
                    if (j != 2)
                      break label172;
                    if (!(paramXmlPullParser.getName().equals("instructions")))
                      break;
                    localDataForm.addInstruction(paramXmlPullParser.nextText());
                  }
                  if (!(paramXmlPullParser.getName().equals("title")))
                    break;
                  localDataForm.setTitle(paramXmlPullParser.nextText());
                }
                if (!(paramXmlPullParser.getName().equals("field")))
                  break;
                localDataForm.addField(parseField(paramXmlPullParser));
              }
              if (!(paramXmlPullParser.getName().equals("item")))
                break;
              localDataForm.addItem(parseItem(paramXmlPullParser));
            }
          while (!(paramXmlPullParser.getName().equals("reported")));
          localDataForm.setReportedData(parseReported(paramXmlPullParser));
        }
      while ((j != 3) || (!(paramXmlPullParser.getName().equals(localDataForm.getElementName()))));
      i = 1;
    }
  }
}